package auto.test.base;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;

import auto.test.util.GetConfigPath;
import auto.test.util.ProUtil;


//用例执行基类
public class BaseCase {
	public WebDriver driver;
    static GetConfigPath getconfigpath=new GetConfigPath();
	static ProUtil pro=new ProUtil(getconfigpath.GetconfigPath());
    private static Properties properties;

	public WebDriver GetDriver(String browser) {

		if (browser.equalsIgnoreCase("chrome")) {
			ChromeOptions options = new ChromeOptions();
			options.setBinary(pro.GetPro("chromepath"));
			options.addArguments("headless");
			options.addArguments("window-size=1920x3000");
			HashMap<String, Object> hashMap = new HashMap<>();
			hashMap.put("download.default_directory", pro.GetPro("setDownloadsPath"));
			hashMap.put("profile.default_content_settings.popups", 0);
			options.setExperimentalOption("prefs", hashMap);
			System.setProperty("webdriver.chrome.driver", pro.GetPro("chromedriver"));
			DesiredCapabilities caps =BaseCase.setDownloadsPath();
			caps.setCapability(ChromeOptions.CAPABILITY,options);
			 driver = new ChromeDriver(caps);
			driver=new ChromeDriver(options);

		}else if (browser.equalsIgnoreCase("chrome1")){
			//常规模式谷歌浏览器
			 System.setProperty("webdriver.chrome.driver", pro.GetPro("chromedriver"));
			ChromeOptions chromeOptions=new ChromeOptions();
			 driver = new ChromeDriver();
		}else{
			//无头模式谷歌浏览器
			System.setProperty("webdriver.chrome.driver",pro.GetPro("chromedriver"));
			ChromeOptions options = new ChromeOptions();
			options.addArguments("--test-type");
			options.addArguments("--headless");
			options.addArguments("--disable-extensions"); //to disable browser extension popup
			options.addArguments("window-size=1920x3000");
			ChromeDriverService driverService = ChromeDriverService.createDefaultService();
			ChromeDriver chromeDriver = new ChromeDriver(driverService, options);
			String downloadPath=pro.GetPro("downloadsPath");
			Map<String, Object> commandParams = new HashMap<>();
			commandParams.put("cmd", "Page.setDownloadBehavior");
			Map<String, String> params = new HashMap<>();
			params.put("behavior", "allow");
			params.put("downloadPath",downloadPath);
//    System.out.println(params.get("downloadPath"));
			commandParams.put("params", params);
			ObjectMapper objectMapper = new ObjectMapper();
			CloseableHttpClient httpClient = HttpClientBuilder.create().build();
			String command = null;
			try {
				command = objectMapper.writeValueAsString(commandParams);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
			String u = driverService.getUrl().toString() + "/session/" + chromeDriver.getSessionId() + "/chromium/send_command";
			HttpPost request = new HttpPost(u);
			request.addHeader("content-type", "application/json");
			try {
				request.setEntity(new StringEntity(command));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			try {
				httpClient.execute(request);
			} catch (IOException e) {
				e.printStackTrace();
			}
			driver=chromeDriver;
		}

		
		return driver;
		
	}
	
	//读取配置文件
	public void Getlog() {
		 PropertyConfigurator.configure(pro.GetPro("logpath"));
		
	}
	public String GetHost(String url) {
		InputStream stream = this.getClass().getClassLoader().getResourceAsStream("env.properties");
		properties = new Properties();
		try {
			properties.load(stream);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String host = properties.getProperty("test.host");
		System.out.println(host);
		return host;
	}
/*
	public static String GetHost1(String url) {
		String host=(pro.GetPro("host")+url);
		return host;
		
		
	}
	*/
	//图片断言方法
	 public boolean IsImageVisible(WebDriver driver,WebElement image){
		  Boolean imageLoaded1 = (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", image); 
		  if (!imageLoaded1) 
		  { 
		  return false;
		  } 
		  return true;
		  }
	
	public static  WebDriver  setDownloadsPath(String downloadsPath) {
    	HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("download.default_directory", downloadsPath);
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", chromePrefs);
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(ChromeOptions.CAPABILITY, options);
        WebDriver driver = new ChromeDriver(caps);
        return driver;
     
    }
//设置下载路路径
	public static DesiredCapabilities setDownloadsPath() {
		String downloadsPath = pro.GetPro("setDownloadsPath");
		HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
		chromePrefs.put("download.default_directory", downloadsPath);
		ChromeOptions options = new ChromeOptions();
		options.setExperimentalOption("prefs", chromePrefs);
		DesiredCapabilities caps = new DesiredCapabilities();
		caps.setCapability(ChromeOptions.CAPABILITY, options);
		return caps;
	}
	  
	    public void max() {
	        driver.manage().window().maximize();
	    }
	    
	    public void quit() {
	        driver.quit();
	       
	        }
	 	    public void Sleep(int i) {
				try {
					Thread.sleep(i);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}


	    public static void main(String[] args) throws InterruptedException {
		}
	    
 }
	


